package com.flying.ouyu.net;

import java.util.HashMap;
import java.util.Map;

import com.flying.client.network.IMsgHandler;
import com.flying.client.network.INetworkSink;
import com.flying.client.network.NetworkClient;
import com.flying.client.network.NetworkResult;
import com.flying.client.network.msg.ORMsg;
import com.flying.client.utils.Logger;
import com.flying.ouyu.net.handler.InitHandler;
import com.flying.ouyu.net.handler.LoginHandler;

public class NetworkSink implements INetworkSink{
	private static Logger logger = Logger.getLogger(NetworkSink.class.getName());;
	
	/*private static Object instanceLocker = new Object();
	private static NetworkSink _instance = null; 
	public static NetworkSink getInstance()
	{
		if (_instance != null)
			return _instance;
		
		synchronized(instanceLocker)
		{
			if (_instance == null)
			{
				_instance = new NetworkSink();
			}
			
			return _instance;
		}
	}
	*/
	
	private Object msgHandlerLocker = new Object();
	private Map<Short,IMsgHandler> msgHandler = new HashMap<Short,IMsgHandler>();
	
	public NetworkSink(){};
	
	public void initMsgType()
	{
		registerMsgHandler(MessageType.Init, InitHandler.getInstance());
		registerMsgHandler(MessageType.Login, LoginHandler.getInstance());
	}
	
	public void registerMsgHandler(short msgType, IMsgHandler handler)
	{
		synchronized(msgHandlerLocker)
		{
			if (!msgHandler.containsKey(msgType))
			{
				msgHandler.put(msgType, handler);
			}
		}
	}
	
	public void unregisterMsgHandler(short msgType) {
		synchronized(msgHandlerLocker)
		{
			if (!msgHandler.containsKey(msgType))
			{
				msgHandler.remove(msgType);
			}
		}
	}
	
	public void onClosed(NetworkResult result) {
		logger.debug("networkCallback onClose result.isSuccess="+result.isSuccess());
		if (result.getClient() != null)
		{
			logger.debug("onClosed client["+result.getClient().serverIp()+":"+result.getClient().serverPort()+"]");
		}
	}

	public void onConnected(NetworkResult result) {
		logger.debug("networkCallback onConnect result.isSuccess="+result.isSuccess());
		if (result.getClient() != null)
		{
			logger.debug("onConnect client["+result.getClient().serverIp()+":"+result.getClient().serverPort()+"]");
		}
		
		if (result.isSuccess())
		{
			//如果连接成功后,发送初始化命令
			InitHandler.getInstance().sendMsg();
		}
	}

	public void onDisConnectioned(NetworkResult result) {
		logger.debug("networkCallback onDisConnection result.isSuccess="+result.isSuccess());
		if (result.getClient() != null)
		{
			logger.debug("onDisConnection client["+result.getClient().serverIp()+":"+result.getClient().serverPort()+"]");
		}
	}

	public void onError(Throwable throwable, NetworkClient client) {
		logger.debug("networkCallback onError e="+throwable);
		
		if (client != null)
		{
			logger.debug("onDisConnection client["+client.serverIp()+":"+client.serverPort()+"]");
		}
	}

	public void onPaused(NetworkResult result) {
		logger.debug("networkCallback onPause result.isSuccess="+result.isSuccess());
		if (result.getClient() != null)
		{
			logger.debug("onPause client["+result.getClient().serverIp()+":"+result.getClient().serverPort()+"]");
		}
	}

	public void onReceivedMsg(ORMsg rMsg) {
		if (rMsg == null)
			return;
		
		synchronized(msgHandlerLocker)
		{
			if (msgHandler.containsKey(rMsg.getMsgType()))
			{
				msgHandler.get(rMsg.getMsgType()).onMsg(rMsg);
			}
			else
			{
				logger.warn("not found msgtype=("+rMsg.getMsgType()+")'s handler");
			}
		}
	}
}
